package com.jeesite.modules.dataview.chaierweijin.service;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.lang.DateUtils;
import com.jeesite.common.service.CrudService;
import com.jeesite.common.service.ServiceException;
import com.jeesite.common.utils.excel.ExcelImport;
import com.jeesite.common.validator.ValidatorUtils;
import com.jeesite.modules.dataview.chaierweijin.dao.DcSupImportDataDao;
import com.jeesite.modules.dataview.chaierweijin.dao.DcSupImportDataTwoDao;
import com.jeesite.modules.dataview.chaierweijin.entity.DcSupImportData;
import com.jeesite.modules.dataview.chaierweijin.entity.ImportResult;
import com.jeesite.modules.home.entity.EchartsDto;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 拆而未尽表Service
 * @author dbl
 * @version 2022-08-04
 */
@Service
@Transactional(readOnly=false)
public class DcSupImportDataTwoService extends CrudService<DcSupImportDataTwoDao, DcSupImportData> {

	public String getNewDate() {
		String newDate ="";
		DcSupImportData dc = dao.getNewDate();
		if(dc!=null){
			newDate= dao.getNewDate().getC28Str();
		}
		return newDate;
	}

	public Page<DcSupImportData> query1(DcSupImportData dcSupImportData) {
		List<DcSupImportData> listC4 = dao.queryGroupC4(dcSupImportData);
		for (int i = 0; i < listC4.size(); i++) {
			Integer num = 0;
			dcSupImportData.setC4(listC4.get(i).getC4());
			List<DcSupImportData> list = dao.query1(dcSupImportData);
			listC4.get(i).setC2(list.get(0).getC2());
			listC4.get(i).setC28Str(DateUtils.formatDate(list.get(0).getC28()));
			for (int j = 0; j < list.size(); j++) {
				if(list.get(j).getC8()==0){
					num++;
				}else{
					continue;
				}
			}
			listC4.get(i).setParam1(num);

		}
		listC4 = listC4.stream().sorted(Comparator.comparing(x -> x.getC2(), Comparator.nullsLast(Comparator.reverseOrder()))).
				collect(Collectors.toList());
		return dcSupImportData.getPage().setList(listC4);
	}
	public Page<DcSupImportData> query2(DcSupImportData dcSupImportData) {
		List<DcSupImportData> listC4 = dao.queryGroupC4(dcSupImportData);
		for (int i = 0; i < listC4.size(); i++) {
			Integer num = 0;
			dcSupImportData.setC4(listC4.get(i).getC4());
			List<DcSupImportData> list = dao.query2(dcSupImportData);
			listC4.get(i).setC2(list.get(0).getC2());
			listC4.get(i).setC28Str(DateUtils.formatDate(list.get(0).getC28()));
			for (int j = 0; j < list.size(); j++) {
				if(list.get(j).getParam1()==1){
					num++;
				}else{
					continue;
				}
			}
			listC4.get(i).setParam1(num);

		}
		listC4 = listC4.stream().sorted(Comparator.comparing(x -> x.getC2(), Comparator.nullsLast(Comparator.reverseOrder()))).
				collect(Collectors.toList());
		return dcSupImportData.getPage().setList(listC4);
	}

	public Map<String,Object> query3(DcSupImportData dcSupImportData) {
		List<EchartsDto> list = dao.query3(dcSupImportData);
		Map<String,Object> map = new HashMap<>();
		map.put("list",list);
		map.put("nameList",list.stream().map(e->e.getName()).collect(Collectors.toList()));
		map.put("valueList",list.stream().map(e->e.getValueDouble()).collect(Collectors.toList()));
		return map;
	}

	public Map<String, Object> query4(DcSupImportData dcSupImportData) {
		dcSupImportData.setC28Str(this.getNewDate());
		List<DcSupImportData> list = dao.query4(dcSupImportData);
		Map<String,Object> map = new HashMap<>();
		List<DcSupImportData> jinhu = list.stream().filter(e->"镜湖区".equals(e.getC2())).collect(Collectors.toList());
		for (int i = 0; i < jinhu.size(); i++) {
			BigDecimal b = new BigDecimal((double)jinhu.get(0).getC10()/jinhu.get(0).getC7()*100);
			b = b.setScale(2,BigDecimal.ROUND_HALF_UP);
			jinhu.get(i).setParam9(b);
		}
		map.put("jinhu",jinhu);
		map.put("nameListjinhu",jinhu.stream().map(e->e.getC28Str()).collect(Collectors.toList()));
		map.put("valueListjinhu",jinhu.stream().map(e->e.getParam9()).collect(Collectors.toList()));
		List<DcSupImportData> yijiang = list.stream().filter(e->"弋江区".equals(e.getC2())).collect(Collectors.toList());
		for (int i = 0; i < yijiang.size(); i++) {
			BigDecimal b = new BigDecimal((double)yijiang.get(0).getC10()/yijiang.get(0).getC7()*100);
			b = b.setScale(2,BigDecimal.ROUND_HALF_UP);
			yijiang.get(i).setParam9(b);
		}
		map.put("yijiang",yijiang);
		map.put("nameListyijiang",yijiang.stream().map(e->e.getC28Str()).collect(Collectors.toList()));
		map.put("valueListyijiang",yijiang.stream().map(e->e.getParam9()).collect(Collectors.toList()));
		List<DcSupImportData> jiujiang = list.stream().filter(e->"鸠江区".equals(e.getC2())).collect(Collectors.toList());
		for (int i = 0; i < jiujiang.size(); i++) {
			BigDecimal b = new BigDecimal((double)jiujiang.get(0).getC10()/jiujiang.get(0).getC7()*100);
			b = b.setScale(2,BigDecimal.ROUND_HALF_UP);
			jiujiang.get(i).setParam9(b);
		}
		map.put("jiujiang",jiujiang);
		map.put("nameListjiujiang",jiujiang.stream().map(e->e.getC28Str()).collect(Collectors.toList()));
		map.put("valueListjiujiang",jiujiang.stream().map(e->e.getParam9()).collect(Collectors.toList()));
		List<DcSupImportData> sanshan = list.stream().filter(e->"三山经济开发区".equals(e.getC2())).collect(Collectors.toList());
		for (int i = 0; i < sanshan.size(); i++) {
			BigDecimal b = new BigDecimal((double)sanshan.get(0).getC10()/sanshan.get(0).getC7()*100);
			b = b.setScale(2,BigDecimal.ROUND_HALF_UP);
			sanshan.get(i).setParam9(b);
		}
		map.put("sanshan",sanshan);
		map.put("nameListsanshan",sanshan.stream().map(e->e.getC28Str()).collect(Collectors.toList()));
		map.put("valueListsanshan",sanshan.stream().map(e->e.getParam9()).collect(Collectors.toList()));
		return map;
	}

	public Map<String, Object> query5(DcSupImportData dcSupImportData) {
		List<EchartsDto> list = dao.query5(dcSupImportData);
		Map<String,Object> map = new HashMap<>();
		for (int i = 0; i < list.size(); i++) {
			BigDecimal b = new BigDecimal((double)list.get(i).getValue()/7*100);
			b = b.setScale(2,BigDecimal.ROUND_HALF_UP);
			list.get(i).setValueDouble(b);
		}
		map.put("list",list);
		map.put("nameList",list.stream().map(e->e.getName()).collect(Collectors.toList()));
		map.put("valueList",list.stream().map(e->e.getValueDouble()).collect(Collectors.toList()));
		return map;
	}

	public Map<String, Object> query6(DcSupImportData dcSupImportData) {
		dcSupImportData.setC28Str(this.getNewDate());
		List<EchartsDto> list = dao.query6(dcSupImportData);
		Map<String,Object> map = new HashMap<>();
		map.put("list",list);
		map.put("nameList",list.stream().map(e->e.getName()).collect(Collectors.toList()));
		map.put("valueList",list.stream().map(e->e.getValue()).collect(Collectors.toList()));
		return map;
	}

	public Page<DcSupImportData> query7(DcSupImportData dcSupImportData) {
		dcSupImportData.setC28Str(this.getNewDate());
		List<DcSupImportData> list =  dao.query7(dcSupImportData);
		for (int i = 0; i < list.size(); i++) {
			if(list.get(i).getC7()==0){
				list.get(i).setParam9(BigDecimal.valueOf(0));
			}else{
				BigDecimal b = new BigDecimal((double)list.get(i).getC9()/list.get(i).getC7()*100);
				b = b.setScale(2,BigDecimal.ROUND_HALF_UP);
				list.get(i).setParam9(b);
			}
		}
		return dcSupImportData.getPage().setList(list);
	}

	public Page<DcSupImportData> query8(DcSupImportData dcSupImportData) {
		dcSupImportData.setC28Str(this.getNewDate());
		List<DcSupImportData> list =  dao.query8(dcSupImportData);
		return dcSupImportData.getPage().setList(list);
	}

	public Map<String, Object> query9(DcSupImportData dcSupImportData) {
		dcSupImportData.setC28Str(this.getNewDate());
		List<EchartsDto> list = dao.query9(dcSupImportData);
		Map<String,Object> map = new HashMap<>();
		map.put("list",list);
		map.put("nameList",list.stream().map(e->e.getName()).collect(Collectors.toList()));
		map.put("valueList",list.stream().map(e->e.getValue()).collect(Collectors.toList()));
		return map;
	}

	public Map<String, Object> query10(DcSupImportData dcSupImportData) {
		List<EchartsDto> list = dao.query10(dcSupImportData);
		Map<String,Object> map = new HashMap<>();
		for (int i = 0; i < list.size(); i++) {
			BigDecimal b = new BigDecimal((double)list.get(i).getValue()/7*100);
			b = b.setScale(2,BigDecimal.ROUND_HALF_UP);
			list.get(i).setValueDouble(b);
		}
		map.put("list",list);
		map.put("nameList",list.stream().map(e->e.getName()).collect(Collectors.toList()));
		map.put("valueList",list.stream().map(e->e.getValueDouble()).collect(Collectors.toList()));
		return map;
	}
}